// newHousePackage/pages/myWallet/myWallet.js


import { myWalletInfo } from "../../../../service/agency_service"
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    restMoney: 0,
    totalMoney: 0,
    cashedMoney: 0,
    isAgent: false,//是否是达人
    isBindMp: false,//是否已经绑定公众号
    roleType: 1,
    showMoney: true,//是否显示money
    showRestMoney: 0,
    showTotalMoney: 0,
    showCashedMoney: 0,
    tempRestMoney: "*",
    tempTotalMoney: "*",
    tempCashedMoney: "*",

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      isBindMp: app.globalData.isBindMp || false,
      roleType: app.globalData.roleType || 1,
    })
  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.myWalletInfo();
    this.setData({
      isAgent: app.globalData.isAgent
    })
  },
  //获取我的钱包数据:
  myWalletInfo() {
    wx.showLoading();
    myWalletInfo().then(res => {
      wx.hideLoading();
      console.log("JJLog:钱包数据:" + JSON.stringify(res));
      if (res.code == 200) {
        this.setData({
          restMoney: res.data.restMoney,
          totalMoney: res.data.totalMoney,
          cashedMoney: res.data.cashedMoney,
          showRestMoney: res.data.restMoney,
          showTotalMoney: res.data.totalMoney,
          showCashedMoney: res.data.cashedMoney
        })
        this.dealMoney(res.data);
      }
    }).catch(err => {
      wx.hideLoading();
      wx.showToast({
        title: err || "查询失败",
        icon: "none"
      })
      console.error("JJLog:myWalletInfo", err);
    })
  },
  //处理金额的隐藏显示
  dealMoney(data) {
    this.setData({
      tempRestMoney: this.replaceStr(data.restMoney) || "*",
      tempTotalMoney: this.replaceStr(data.totalMoney) || "*",
      tempCashedMoney: this.replaceStr(data.cashedMoney) || "*"
    })
  },
  replaceStr(str) {
    let resultStr = "";
    let aStr = JSON.stringify(str);
    for (let i = 0; i < aStr.length; i++) {
      resultStr = resultStr + "*";
    }
    console.log("JJLog:resultStr:" + resultStr);
    return resultStr

  },



  pushToIncomeList() {
    wx.navigateTo({
      url: '/formPackage/pages/agencyModule/agencyIncomeList/agencyIncomeList',
    })
  },


  //跳转到提现去:
  withdrawAction() {

    // if (this.data.restMoney < 100) {
    //   wx.showToast({
    //     title: '余额超过100元才能提现',
    //     icon: "none",
    //     duration: 3000
    //   })
    //   return;
    // }
    if (this.data.roleType == 7 || this.data.roleType == 9) {
      wx.navigateTo({
        url: '/formPackage/pages/agencyModule/agencyWithdraw/agencyWithdraw?money=' + this.data.restMoney,
      })
    } else {
      wx.showToast({
        title: '申请入驻才能提现',
        icon: "none"
      })
      return;
    }

  },
  //跳转到公众号页面:
  pushToOfficalAction() {
    wx.navigateTo({
      url: '/firstPackage/pages/officialAccounts/officialAccounts',
    })
  },

  moneyShowAction() {
    let isShow = this.data.showMoney;
    if (isShow) {
      isShow = false
      this.setData({
        showRestMoney: this.data.tempRestMoney,
        showTotalMoney: this.data.tempTotalMoney,
        showCashedMoney: this.data.tempCashedMoney,
      })
    } else {
      isShow = true
      this.setData({
        showRestMoney: this.data.restMoney,
        showTotalMoney: this.data.totalMoney,
        showCashedMoney: this.data.cashedMoney,
      })
    }
    this.setData({
      showMoney: isShow
    })
  },




})